#ifndef RULES_C
#define RULES_C
#include "rules.h"

static unsigned char protocols[] = {PROT_ICMP,PROT_TCP,PROT_UDP,LAST_RULE_PROT};	// Our protocols

static unsigned int fw_config = 0; // firewall config 

static rule_t* rulesPtr = NULL;    // Global variable for rules pointer

static void loadDefaultRules(void){
	rule_t defaultRules[NUM_OF_DEF_RULES] = { { 0 } };
	int i;		
	// construct default rules
	for(i=0;i<NUM_OF_DEF_RULES;i++){
		defaultRules[i].protocol = protocols[i];
		defaultRules[i].src_ip = LOCALHOST;
		defaultRules[i].dst_ip = LOCALHOST;
		defaultRules[i].src_port = PORT_WILDCARD;
		defaultRules[i].dst_port = PORT_WILDCARD;
		defaultRules[i].src_mask = MAX_BITS_MASK;
		defaultRules[i].dst_mask = MAX_BITS_MASK;
		defaultRules[i].action = NF_ACCEPT;
	}	
	// copy rules 
	if(rulesPtr != NULL)
		memcpy(rulesPtr,defaultRules,sizeof(rule_t)*NUM_OF_DEF_RULES);		
	else
		printk("\n rulesPtr is NULL \n");
}



static char checkPermission(rawPacket_t* packet,int *matchedIndex){
	rule_t* nextRule = rulesPtr;
	int count = 0;
	int sPort = 0, dPort = 0; 
	// Iterate over rules array to find the correct rule
	while(nextRule->protocol != LAST_RULE_PROT && count < RULE_BASE_ENTRIES){				
		if(nextRule->protocol == packet->protocol){
			if(ntohl(nextRule->src_ip)  == applyMaskToIP(packet->source_ip,nextRule->src_mask)  && 
			   ntohl(nextRule->dst_ip)  == applyMaskToIP(packet->dest_ip,nextRule->dst_mask)){
				sPort = (nextRule->src_port == 0)?1:(ntohs(nextRule->src_port) == packet->source_port)?1:0;
				dPort = (nextRule->dst_port == 0)?1:(ntohs(nextRule->dst_port) == packet->dest_port)?1:0;
				if(sPort && dPort){			
					// Rule applied						
					*matchedIndex = count;
					return nextRule->action;	
				}
			}
		}
		++nextRule;
		++count;
	}
	// Cannot find any matched rule
	return UNMATCHED_PACKET;
}

	

#endif
